package com.school.graduation.ui.view.bottombar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.school.graduation.R;

public class BottomBar extends RelativeLayout implements View.OnClickListener{

    private LinearLayout home,cascades,people;
    private ImageView home_img,cascades_img,people_img;
    private TextView home_text,cascades_text,people_text;
    private OnBottomBarClickListener listener;

    public BottomBar(Context context) {
        super(context);
        init();
        setListener();
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context,attrs);
        init();
        setAttrs(context,attrs);
        setListener();
    }

    public BottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setAttrs(context,attrs);
        init();
        setListener();
    }

    public void setFirstImg(int res){
        home_img.setImageResource(res);
    }

    public void setSecondImg(int res){
        cascades_img.setImageResource(res);
    }

    public void setFourthImg(int res){
        people_img.setImageResource(res);
    }

    public void setFirstText(String text){
        home_text.setText(text);
    }

    public void setSecondText(String text){
        cascades_text.setText(text);
    }

    public void setFourthText(String text){
        people_text.setText(text);
    }

    public String getFirstText(){
        return home_text.getText().toString();
    }

    public String getSecondText(){
        return cascades_text.getText().toString();
    }

    public String getFourthText(){
        return people_text.getText().toString();
    }

    public void setTextColor(int position,int res){
        switch (position){
            case 0:
                home_text.setTextColor(getResources().getColor(res));
                break;
            case 1:
                cascades_text.setTextColor(getResources().getColor(res));
                break;
            case 2:
                people_text.setTextColor(getResources().getColor(res));
                break;
        }
    }

    //设置点击事件
    private void setListener() {
        home.setOnClickListener(this);
        cascades.setOnClickListener(this);
        people.setOnClickListener(this);
    }

    //设置用户在xml配置的属性
    private void setAttrs(Context context,AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.BottomBar,0,0);
        int firstRes = array.getResourceId(R.styleable.BottomBar_first_img,R.mipmap.home);
        int secondRes = array.getResourceId(R.styleable.BottomBar_second_img,R.mipmap.cascades);
        int fourthRes = array.getResourceId(R.styleable.BottomBar_fourth_img,R.mipmap.people);
        String firstText = array.getString(R.styleable.BottomBar_first_text);
        String secondText = array.getString(R.styleable.BottomBar_second_text);
        String fourthText = array.getString(R.styleable.BottomBar_fourth_text);
        home_img.setImageResource(firstRes);
        cascades_img.setImageResource(secondRes);
        people_img.setImageResource(fourthRes);
        home_text.setText(firstText);
        cascades_text.setText(secondText);
        people_text.setText(fourthText);
        array.recycle();
    }

    //初始化视图view
    private void init() {
        View view = View.inflate(getContext(),R.layout.bottom_bar,this);
        home_img = (ImageView) view.findViewById(R.id.home_img);
        cascades_img = (ImageView) view.findViewById(R.id.cascades_img);
        people_img = (ImageView) view.findViewById(R.id.people_img);
        home_text = (TextView) view.findViewById(R.id.home_text);
        cascades_text = (TextView) view.findViewById(R.id.cascades_text);
        people_text = (TextView) view.findViewById(R.id.people_text);
        home = (LinearLayout) view.findViewById(R.id.home_layout);
        cascades = (LinearLayout) view.findViewById(R.id.cascades_layout);
        people = (LinearLayout) view.findViewById(R.id.people_layout);
    }

    public void setOnBottomBarClickListener(OnBottomBarClickListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.home_layout:
                if (listener!=null) listener.clickFirstItem();
                break;
            case R.id.cascades_layout:
                if (listener!=null) listener.clickSecondItem();
                break;
            case R.id.people_layout:
                if (listener!=null) listener.clickFourthItem();
                break;
        }
    }
}
